package converter

import (
	"bytes"
	"encoding/binary"
	"unsafe"
)

func StrToBytes(s string) []byte {
	x := (*[2]uintptr)(unsafe.Pointer(&s))
	h := [3]uintptr{x[0], x[1], x[1]}
	return *(*[]byte)(unsafe.Pointer(&h))
}

func BytesToStr(b []byte) string {
	return *(*string)(unsafe.Pointer(&b))
}

func Int8ToBytes(n int8) []byte {
	buf := bytes.NewBuffer(make([]byte, 0, 1))
	_ = binary.Write(buf, binary.BigEndian, n)
	return buf.Bytes()
}

func Int16ToBytes(n int16) []byte {
	buf := bytes.NewBuffer(make([]byte, 0, 2))
	_ = binary.Write(buf, binary.BigEndian, n)
	return buf.Bytes()
}

func Int32ToBytes(n int32) []byte {
	buf := bytes.NewBuffer(make([]byte, 0, 4))
	_ = binary.Write(buf, binary.BigEndian, n)
	return buf.Bytes()
}

func Int64ToBytes(n int64) []byte {
	buf := bytes.NewBuffer(make([]byte, 0, 8))
	_ = binary.Write(buf, binary.BigEndian, n)
	return buf.Bytes()
}

func BytesToInt8(bys []byte) int8 {
	buf := bytes.NewBuffer(bys)
	var data int8
	_ = binary.Read(buf, binary.BigEndian, &data)
	return data
}

func BytesToInt16(bys []byte) int16 {
	buf := bytes.NewBuffer(bys)
	var data int16
	_ = binary.Read(buf, binary.BigEndian, &data)
	return data
}

func BytesToInt32(bys []byte) int32 {
	buf := bytes.NewBuffer(bys)
	var data int32
	_ = binary.Read(buf, binary.BigEndian, &data)
	return data
}

func BytesToInt64(bys []byte) int64 {
	buf := bytes.NewBuffer(bys)
	var data int64
	_ = binary.Read(buf, binary.BigEndian, &data)
	return data
}

func UInt8ToBytes(n uint8) []byte {
	buf := bytes.NewBuffer(make([]byte, 0, 1))
	_ = binary.Write(buf, binary.BigEndian, n)
	return buf.Bytes()
}

func UInt16ToBytes(n uint16) []byte {
	buf := bytes.NewBuffer(make([]byte, 0, 2))
	_ = binary.Write(buf, binary.BigEndian, n)
	return buf.Bytes()
}

func UInt32ToBytes(n uint32) []byte {
	buf := bytes.NewBuffer(make([]byte, 0, 4))
	_ = binary.Write(buf, binary.BigEndian, n)
	return buf.Bytes()
}

func UInt64ToBytes(n uint64) []byte {
	buf := bytes.NewBuffer(make([]byte, 0, 8))
	_ = binary.Write(buf, binary.BigEndian, n)
	return buf.Bytes()
}

func BytesToUInt8(bys []byte) uint8 {
	buf := bytes.NewBuffer(bys)
	var data uint8
	_ = binary.Read(buf, binary.BigEndian, &data)
	return data
}

func BytesToUInt16(bys []byte) uint16 {
	buf := bytes.NewBuffer(bys)
	var data uint16
	_ = binary.Read(buf, binary.BigEndian, &data)
	return data
}

func BytesToUInt32(bys []byte) uint32 {
	buf := bytes.NewBuffer(bys)
	var data uint32
	_ = binary.Read(buf, binary.BigEndian, &data)
	return data
}

func BytesToUInt64(bys []byte) uint64 {
	buf := bytes.NewBuffer(bys)
	var data uint64
	_ = binary.Read(buf, binary.BigEndian, &data)
	return data
}
